﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Configuration;

public static class SSCCDatabase
{
    public static Query Query(string QueryString)
    {
        return new Query(QueryString, new Dictionary<SqlParameter,object>());
    }
    public static Query Query(string QueryString, SqlParameter Parameter, object Value)
    {
        return new Query(QueryString, new Dictionary<SqlParameter,object>(){{Parameter,Value}});
    }
    public static Query Query(string QueryString, Dictionary<SqlParameter, object> Parameters)
    {
        return new Query(QueryString, Parameters);
    }

    public static int ExecuteStatement(string QueryString)
    {
        return ExecuteStatement(QueryString, new Dictionary<SqlParameter, object>());
    }
    public static int ExecuteStatement(string QueryString, SqlParameter Parameter, object Value)
    {
        return ExecuteStatement(QueryString, new Dictionary<SqlParameter, object>() { { Parameter, Value } });
    }
    public static int ExecuteStatement(string QueryString, Dictionary<SqlParameter, object> Parameters)
    {

        ConnectionStringSettings ConnectionStringSettings = ConfigurationManager.ConnectionStrings["mainConn"];

        if (ConnectionStringSettings == null || ConnectionStringSettings.ConnectionString.Trim() == "")
        {
            throw new Exception("Connection string cannot be blank.");
        }

        var connectionString = ConnectionStringSettings.ConnectionString;


        var conn = new SqlConnection(connectionString);
        conn.Open();

        SqlCommand cmd = new SqlCommand(QueryString, conn);

        foreach (var item in Parameters)
        {
            cmd.Parameters.Add(item.Key).Value = item.Value;
        }

        int ret = cmd.ExecuteNonQuery();
        conn.Close();
        return ret;
    }

    public static int HighestID(string TableName)
    {
        using (var Q = Query("SELECT ID FROM " + TableName + " ORDER BY ID DESC"))
        {
            return (int)Q.First()["ID"];
        }
    }
}

public class Params:Dictionary<SqlParameter, object>
{

}